<?php

namespace backend\controllers;

use Yii;
use app\models\Category;
//use app\models\CategorySub;
//use app\models\CategorySubSub;
use yii\base\ErrorException;
use yii\base\Exception;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * MdspartsController implements the CRUD actions for Mdsparts model.
 */
class CategoryController extends Controller
{
    public $layout = "content_parts";

    public function actionList(){


        $get = Yii::$app->request->get();
        $level = isset( $get['level'] ) ? $get['level'] : 0;

        $searchModel = Category::getCategoryByLevel( );
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('list', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'level' => $level,
        ]);
    }

    public function actionView($id){

        $get = Yii::$app->request->get();
        $level = isset( $get['level'] ) ? $get['level'] : 0;
        $childLevel = intval( $level ) + 1;
        $parentLevel = intval( $level ) - 1;
        try{


            $category = Category::getCategoryByLevel( );
            $model= $category->findOne(array('id'=>$id));


        }catch( Exception $e ){
            echo $e->getMessage();
            die();
        }
        $childCategories = array();
        if( $childLevel <= 2){
            $childCategories = Category::getChildCategory($id, $childLevel);
        }
        $parentCategory = array();
        if( $level >= 1 ){
            $parentModel = Category::getCategoryByLevel( $parentLevel );
            $parent_id = $level == 1 ? $model->category_id : $model->category_sub_id;
            $parentCategory = $parentModel->findOne( $parent_id );
        }
        return $this->render('view', [
            'model' => $model,
            'parentCategory' => $parentCategory,
            'childCategories' => $childCategories,
            'level' => $level,
            'childLevel' => $childLevel,
            'parentLevel' => $parentLevel
        ]);

    }

    public function actionCreate(){

        $model = Category::getCategoryByLevel( );
        if( Yii::$app->request->getMethod() == 'POST' ){

            $post = Yii::$app->request->post();

            $level = isset($post['Category']['level']) ?
                $post['Category']['level']: 0;
            $model = Category::getCategoryByLevel( $level );

            switch( $level ){
                case  1 :
                    $post['Category']['category_id'] = $post['Category']['parent'];
                    break;
                case  2 :
                    $post['Category']['category_sub_id'] = $post['Category']['parent'];
                    break;
                default :
                    break;
            }

            $tables = [ 'Category','CategorySub','CategorySubSub' ];
            $data[ $tables[$level] ] =  $post['Category'];
//            unset($data['parent_id']);
            if ($model->load( $data ) && $model->save()) {
                $params = array(
                    'view',
                    'id' => $model->id,
                    'level' => $level
                );


                return $this->redirect( $params );
            } else {
                $errors = $model->getErrors();
                print_r( $errors );
                die();
            }
        }

        return $this->render('create', [
            'model' => $model
        ]);

    }

    public function actionUpdate( $id, $level ){

        $get = Yii::$app->request->get();
        $level = isset( $get['level'] ) ? $get['level'] : 0;
        $childLevel = intval( $level ) + 1;
        $parentLevel = intval( $level ) - 1;

        $category = Category::getCategoryByLevel( $level );

        $model = $category->findOne($id);
        if( Yii::$app->request->getMethod() == 'POST' ) {

            $post = Yii::$app->request->post();
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect([
                    'view',
                    'id' => $model->id,
                    'level' => $level
                ]);
            } else {

            }
        }

        $childCategories = array();
        if( $childLevel <= 2){
            $childCategories = Category::getChildCategory($id, $childLevel);
        }
        $parentCategory = array();
        if( $level >= 1 ){
            $parentModel = Category::getCategoryByLevel( $parentLevel );
            $parent_id = $level == 1 ? $model->category_id : $model->category_sub_id;
            $parentCategory = $parentModel->findOne( $parent_id );
        }
        return $this->render('update', [
            'model' => $model,
            'parentCategory' => $parentCategory,
            'childCategories' => $childCategories,
            'level' => $level,
            'childLevel' => $childLevel,
            'parentLevel' => $parentLevel
        ]);




    }

    public function actionDelete( $id ){
        $get = Yii::$app->request->get();
        $level = isset( $get['level'] ) ? $get['level'] : 0;

        $category = Category::getCategoryByLevel( $level );
        $category->findOne($id)->delete();

        return $this->redirect(['list','level' => $level]);
    }

    /**
     *
     */
    public function actionChild( $id, $level ){

        $res = array(
            'status'=>0
        );
        try{

            $children = Category::getChildCategory($id, $level);
            $res['status'] = 1;
            $res['category'] = $children;
        }catch( Exception $e ){
            $res['description'] = $e->getMessage();
        }

        header("Content-type:application/json;charset=utf-8");
        echo json_encode( $res );
        die();
    }
}
